﻿using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reborn.Net.Http.Extension
{
    public static class RestSharpExtension
    {

        public static async Task<Response> TryGetAsync(this RestClient restClient, RestRequest restRequest)
        {
            Response response = new Response();
            try
            {
                response.StatusCode = System.Net.HttpStatusCode.OK;
                var result =  await restClient.ExecuteGetAsync(restRequest);
                response.Content = result.Content;

                return response;
            }
            catch (System.Net.Http.HttpRequestException ex)
            {
                MessagePipelines.Error?.Invoke(null, ex);

                response.StatusCode = System.Net.HttpStatusCode.BadRequest;
                response.Content = ex.Message;

                return response;
            }
            catch (Exception ex)
            {
                MessagePipelines.Error?.Invoke(null, ex);

                response.StatusCode = System.Net.HttpStatusCode.BadRequest;
                response.Content = ex.Message;

                return response;
            }
        }

        public static async Task<Response> TryPostAsync(this RestClient restClient, RestRequest restRequest)
        {
            Response response = new Response();
            try
            {
                response.StatusCode = System.Net.HttpStatusCode.OK;

                var result =  await restClient.ExecutePostAsync(restRequest);
                response.Content = result.Content;

                return response;

            }
            catch (System.Net.Http.HttpRequestException ex)
            {
                MessagePipelines.Error?.Invoke(null, ex);

                response.StatusCode = System.Net.HttpStatusCode.BadRequest;
                response.Content = ex.Message;

                return response;
            }
            catch (Exception ex)
            {
                MessagePipelines.Error?.Invoke(null, ex);

                response.StatusCode = System.Net.HttpStatusCode.BadRequest;
                response.Content = ex.Message;
                return response;
            }
        }

        public static Response TryPost(this RestClient restClient, RestRequest restRequest)
        {
            Response response = new Response();
            try
            {
                response.StatusCode = System.Net.HttpStatusCode.OK;

                var result = restClient.Post(restRequest);
                response.Content = result.Content;

                return response;

            }
            catch (System.Net.Http.HttpRequestException ex)
            {
                MessagePipelines.Error?.Invoke(null, ex);

                response.StatusCode = System.Net.HttpStatusCode.BadRequest;
                response.Content = ex.Message;

                return response;
            }
            catch (Exception ex)
            {
                MessagePipelines.Error?.Invoke(null, ex);

                response.StatusCode = System.Net.HttpStatusCode.BadRequest;
                response.Content = ex.Message;
                return response;
            }
        }
    }
}
